import { ref } from 'vue';
import StudioOperate, { OperateType } from 'src/models/StudioOperate';
import Stack from 'src/utils/Stack';
const undoStack: Stack<StudioOperate> = new Stack<StudioOperate>();
const redoStack: Stack<StudioOperate> = new Stack<StudioOperate>();
const canUndo = ref(false);
const canRedo = ref(false);
export default function () {
  function undo() {
    if (canUndo.value) {
      const action = undoStack.Pop();
      if (action) {
        redoStack.Push(action);
        refresh();
        return action;
      }
    }
  }
  function redo() {
    if (canRedo.value) {
      const action = redoStack.Pop();
      if (action) {
        undoStack.Push(action);
        refresh();
        return action;
      }
    }
  }
  function addAction(action: any) {
    undoStack.Push(action);
    refresh();
  }
  function refresh() {
    if (undoStack.Count > 0) {
      canUndo.value = true;
    } else {
      canUndo.value = false;
    }
    if (redoStack.Count > 0) {
      canRedo.value = true;
    } else {
      canRedo.value = false;
    }
  }
  return {
    canUndo,
    canRedo,
    undo,
    redo,
    addAction,
  };
}
